﻿/////////////////////////////////////////////////////////////////
//     .--.
//    |o_o |    NASDAQRSSReader.cs
//    |:_/ |        Implementación de la clase NASDAQRSSReader
//   //   \ \   Created on:
//  (|     | )      18-abr-2009 21:15:50
// /'\_   _/`\  Original author:
// \___)=(___/      The SoftTux Team
/////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RSSReader
{
    class NASDAQRSSReader
    {

        string[] big10;

        /// <summary>
        /// constructor de el parseador RSS para NASDAQ
        /// </summary>
        /// <param name="big10">un array de 10 acrónimos de empresas</param>
        public NASDAQRSSReader(string[] big10)
        {
            this.big10 = big10;
        }


        /// <summary>
        /// crea la url para obtener las cotizaciones
        /// </summary>
        /// <returns>la url</returns>
        public string NASDAQ_stocks_url()
        {
            string output = "http://www.nasdaq.com/aspxcontent/NasdaqRSS.aspx?data=quotes";
            foreach (string u in this.big10)
            {
                output += "&symbol=" + u;
            }
            return output;
        }

        /// <summary>
        /// parsea el resultado obtenido de la url especificada
        /// </summary>
        /// <param name="url">la url para obtener las cotizaciones</param>
        /// <returns>el array de stocks</returns>
        public StockADT[] parse1(string url)
        {
            XmlDocument RSSXml = new XmlDocument();
            RSSXml.Load(url);
            XmlNodeList RSSNodeList = RSSXml.SelectNodes("rss/channel/item");

            StockADT[] stocks = new StockADT[this.big10.Length];
            for (int i = 0; i < stocks.Length; i++)
            {
                stocks[i] = new StockADT(this.big10[i]);
            }
            int idx = 0, ids = 0;
            foreach (XmlNode RSSNode in RSSNodeList)
            {
                XmlNode RSSSubNode;
                RSSSubNode = RSSNode.SelectSingleNode("description");
                string desc = RSSSubNode != null ? RSSSubNode.InnerText : "";

                char[] seps = { ',', '\n', '\r' };
                String[] s1 = desc.Split(seps);

                string sPattern = "<TD nowrap";

                foreach (String s in s1)
                {
                    char[] arr = new char[] { '\t', ',', ' ' };
                    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {//([\S\s]*?)
                        string s2 = System.Text.RegularExpressions.Regex.Replace(s, "<TD nowrap width=\"50%\">&nbsp;", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "<TD nowrap align=right width=\"50%\">", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "<font color=\"green\">", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "<font color=\"red\">", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "<img src=http://www.nasdaq.com/images/greenarrowsmall.gif >", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "<img src=http://www.nasdaq.com/images/redarrowsmall.gif >", "");
                        s2 = System.Text.RegularExpressions.Regex.Replace(s2, "</font>&nbsp;", "");

                        if (System.Text.RegularExpressions.Regex.IsMatch(s2, "<TD nowrap>&nbsp;As of:", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                        {
                            // no hacemos nada
                        }
                        else
                        {
                            char[] x = s2.ToCharArray();

                            idx++;
                            string word = "";
                            for (int q = 0; q < x.Length; q++)
                            {
                                char current = x[q];

                                switch (current)
                                {
                                    case ' ':
                                        break;
                                    case '\t':
                                        break;
                                    case '%':
                                        break;

                                    default:
                                        word += current;
                                        break;
                                }
                            }

                            switch (idx)
                            {
                                case 2:
                                    word = word.Replace('.', ',');
                                    stocks[ids].Last = double.Parse(word);
                                    break;
                                case 4:
                                    word = word.Replace('.', ',');
                                    stocks[ids].Change = double.Parse(word);
                                    break;
                                case 6:
                                    word = word.Replace('.', ',');
                                    stocks[ids].Changep = double.Parse(word);
                                    break;
                                case 8:
                                    stocks[ids].Volume = int.Parse(word);
                                    break;
                            }
                            if (idx == 8)
                            {
                                idx = 0;
                                ids++;
                            }
                        }
                    }
                    else
                    {
                        // no hacemos nada
                    }
                }
            }
            return stocks;
        }
    }
}